import * as THREE from 'three'
import { DIMENSIONS, LEG_LAYOUTS, BOTTOM_TYPES, PANEL_TYPES } from './constants'

export const createTwoWayPallet = (scene, material, params) => {
  const palletGroup = scene.getPalletGroup()
  const {
    BOARD_THICKNESS,
    BEAM_WIDTH,
    BEAM_HEIGHT,
    SUPPORT_HEIGHT,
    SUPPORT_THICKNESS
  } = DIMENSIONS

  // 参数转换 
  const scaledLength = params.boardLength / 100
  const scaledBeamLength = params.beamLength / 100
  const scaledWingWidth = params.tf_boardwing / 100

  // 计算木方位置
  let beamPositions = []
  if (params.customize === 0) { 
    // 二手模式 - 默认3条木方
    beamPositions = [
      -scaledLength / 2 + scaledWingWidth,
      0,
      scaledLength / 2 - scaledWingWidth
    ]
  } else {
    // 定制模式 - 根据 t_leglayout 决定
    if (params.t_leglayout === LEG_LAYOUTS.THREE || !params.t_leglayout) {
      // 3条木方 或 默认
      beamPositions = [
        -scaledLength / 2 + scaledWingWidth,
        0,
        scaledLength / 2 - scaledWingWidth
      ]
    } else if (params.t_leglayout === LEG_LAYOUTS.FOUR) {
      // 4条木方
      const totalLength = scaledLength - 2 * scaledWingWidth
      const leftPos = -scaledLength / 2 + scaledWingWidth
      beamPositions = [
        leftPos,
        leftPos + totalLength * 0.4,
        leftPos + totalLength * 0.6,
        leftPos + totalLength
      ]
    } else {
      // 未知布局，使用默认3条
      beamPositions = [
        -scaledLength / 2 + scaledWingWidth,
        0,
        scaledLength / 2 - scaledWingWidth
      ]
    }
  }



  beamPositions.forEach(pos => {
    const beam = new THREE.Mesh(
      new THREE.BoxGeometry(BEAM_WIDTH, BEAM_HEIGHT, scaledBeamLength),
      material
    )
    beam.position.set(pos, -BEAM_HEIGHT / 2, 0)
    beam.castShadow = beam.receiveShadow = true
    palletGroup.add(beam)
  })


  // 创建顶部面板 
  createTopPanel(scene, material, {
    panel_leg: params.panel_leg,
    boardLength: params.boardLength,
    beamLength: params.beamLength,
    tf_boardwing: params.tf_boardwing
  }, scaledLength, scaledBeamLength)

  // 创建底部支撑（仅定制产品且选择底部支撑时）
  if (params.customize === 1 && params.t_bottomlayer === BOTTOM_TYPES.SUPPORT) {
    createBottomSupports(scene, material, scaledLength, scaledBeamLength)
  }
}

const createTopPanel = (scene, material, params, scaledLength, scaledBeamLength) => {
  const palletGroup = scene.getPalletGroup()
  const { BOARD_THICKNESS, BEAM_HEIGHT } = DIMENSIONS

  if (params.panel_leg === PANEL_TYPES.PLYWOOD) {
    // 胶合板模式 - 整块面板 
    const panel = new THREE.Mesh(
      new THREE.BoxGeometry(scaledLength + 0.05, BOARD_THICKNESS, scaledBeamLength),
      material
    )
    panel.position.set(0, BEAM_HEIGHT / 2 + BOARD_THICKNESS / 2 - 0.05, 0)
    panel.castShadow = panel.receiveShadow = true
    palletGroup.add(panel)
  } else {
    // 板条-原木模式 - 多块木板 
    const boardCount = 6
    const minBoardWidth = 0.01
    const baseBoardWidth = 0.1
    const minGap = 0.005

    let boardWidth, gap
    const totalSpace = scaledBeamLength

    if (totalSpace <= boardCount * minBoardWidth) {
      boardWidth = minBoardWidth
      gap = (totalSpace - (boardCount * boardWidth)) / (boardCount - 1)
    } else {
      boardWidth = baseBoardWidth
      gap = (totalSpace - (boardCount * boardWidth)) / (boardCount - 1)

      if (gap < minGap) {
        gap = minGap
        boardWidth = (totalSpace - ((boardCount - 1) * gap)) / boardCount
      }
    }

    for (let i = 0; i < boardCount; i++) {
      const board = new THREE.Mesh(
        new THREE.BoxGeometry(scaledLength + 0.05, BOARD_THICKNESS, boardWidth),
        material
      )
      board.position.set(
        0,
        BEAM_HEIGHT / 2 + BOARD_THICKNESS / 2 - 0.05,
        -totalSpace / 2 + boardWidth / 2 + i * (boardWidth + gap)
      )
      board.castShadow = board.receiveShadow = true
      palletGroup.add(board)
    }
  }
}

const createBottomSupports = (scene, material, scaledLength, scaledBeamLength) => {
  const palletGroup = scene.getPalletGroup()
  const { BEAM_HEIGHT, SUPPORT_HEIGHT, SUPPORT_THICKNESS } = DIMENSIONS

  const supportCount = 3
  const startZ = -scaledBeamLength / 2 + SUPPORT_HEIGHT
  const endZ = scaledBeamLength / 2 - SUPPORT_HEIGHT
  const stepZ = (endZ - startZ) / (supportCount - 1)

  for (let i = 0; i < supportCount; i++) {
    const support = new THREE.Mesh(
      new THREE.BoxGeometry(scaledLength, SUPPORT_HEIGHT, SUPPORT_THICKNESS),
      material
    )
    support.position.set(
      0,
      -BEAM_HEIGHT / 2 - SUPPORT_HEIGHT / 2 - 0.04,
      startZ + i * stepZ
    )
    support.castShadow = support.receiveShadow = true
    palletGroup.add(support)
  }
}